package n_06;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChengMiao
 * @date 2021/6/29 15:48
 * @Desc No.6 字形变换
 * 将一个给定字符串 s 根据给定的行数 numRows ，以从上往下、从左到右进行 Z 字形排列。
 */
public class Demo {
    public static void main(String[] args) {
        String str = "PAYPALISHIRING";

//        P A Y P A L I S H I R  I  N  G
//        0 1 2 3 4 5 6 7 8 9 10 11 12 13
//        P   A   H   N
//        A P L S I I G
//        Y   I   R
        int row = 3;

        String convert = convert2(str, row);
        System.out.println(convert);

    }

    public static String convert2(String s, int numRows) {

        if(s.length()<=numRows ||numRows<=1){
            return s;
        }

        List<StringBuilder> rows = new ArrayList<>();
        for (int i = 0; i < Math.min(s.length(),numRows); i++) {
            rows.add(new StringBuilder());
        }

        int curRow = 0;
        boolean isDown = false;

        for (char c : s.toCharArray()) {
            rows.get(curRow).append(c);
            if(curRow==0 || curRow==numRows-1){
                isDown = !isDown;
            }

            curRow += isDown?1:-1;
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (StringBuilder row : rows) {
            System.out.println(row);
            stringBuilder.append(row);
        }

        return stringBuilder.toString();

    }

    public static String convert(String s, int numRows) {

        if(s.length()<=numRows ||numRows<=1){
            return s;
        }

        String arr[][] = new String[numRows][s.length()];

        char[] chars = s.toCharArray();

        int k = 0;
        int f = 0;
        boolean isDown = true;
        for (int i = 0; i < chars.length; i++) {

            arr[k][f] = String.valueOf(chars[i]);
            if(isDown){
                k++;
            }else{
                k--;
                f++;
            }

            if(k==numRows-1){
                isDown = false;
            }

            if(k==0){
                isDown = true;
            }


        }

        String str = "";
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j <arr[i].length ; j++) {

                if(arr[i][j]!=null){
                    str += String.valueOf(arr[i][j]);
                    System.out.print(arr[i][j] +"\t");
                }else{
                    System.out.print("  \t");
                }
            }
            System.out.println();
        }

        return str;
    }

    // 我的
    public static String convert3(String s, int numRows) {

        int step1 = numRows + (numRows - 3 +1);
        int step2 = 0;

        int length = s.length();
        char[] chars = s.toCharArray();
        if(length<=2 || numRows==1){
            return s;
        }

        // 列最大长度
        int size = length/numRows +1;

        StringBuilder s1 = new StringBuilder();


        // 读取每一行
        for (int i = 0; i < numRows; i++) {
            int j = i;
            int k = 0;
            while(j< length){
                s1.append(chars[j]);
                // 计算下一个指针位置
                if((k%2==0 || step2==0) &&  i!=numRows-1){
                    j += step1;
                }else{
                    j += step2;
                }
                k++;
            }
            step1 -=2;
            step2 +=2;
        }


        return s1.toString();

    }
}
